<template>
  <el-dialog
    title="分配角色"
    :visible="visible"
    @close="closeFn"
    @open="openFn"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button
        type="primary"
        @click="submit"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getDetail } from '@/api/user'
import { assignRole } from '@/api/employees'

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  methods: {
    closeFn() {
      this.$emit('update:visible', false)
      this.checkList = []
    },

    openFn() {
      this.getRoleList()
      this.getDetail()
    },
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    async getDetail() {
      const { roleIds } = await getDetail(this.employeeId)
      this.checkList = roleIds || []
    },
    cancel() {
      this.closeFn()
    },
    async submit() {
      if (!this.checkList.length) return this.$message.warning('请选择角色哦~~~')
      await assignRole({
        id: this.employeeId,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功了哦~~')
      this.closeFn()
    }
  }
}
</script>
